package com.apps.diet.endpoint;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import com.apps.diet.oxm.DeleteMealsRequest;
import com.apps.diet.oxm.DeleteMealsResponse;
import com.apps.diet.service.MealsService;


/**
 * Handles meals requests. 
 * <p>
 *<pre>
 * @Endpoint: similar to Spring MVC's @Controller. 
 * @PayloadRoot: similar to Spring MVC's @RequestMapping</pre>
 */
@Endpoint
public class DeleteMealsEndpoint {

	protected static Logger logger = Logger.getLogger(DeleteMealsEndpoint.class);

	@Resource(name="mealsService")
	private MealsService mealsService;

	public static final String NAMESPACE_URI = "http://diet.apps.com/ws/schema/diet";

	public static final String REQUEST_LOCAL_NAME = "deleteMealsRequest";

	@PayloadRoot(localPart = REQUEST_LOCAL_NAME, namespace = NAMESPACE_URI)
	@ResponsePayload
	public DeleteMealsResponse deleteAllMeals( @RequestPayload DeleteMealsRequest deleteMealsRequest) {
		try {
			int mealId = deleteMealsRequest.getMealId();
			mealsService.deleteMeals(mealId);
		}  catch (Exception e) {
			logger.error(e.getMessage());
			DeleteMealsResponse response = new DeleteMealsResponse();
			response.setCode("FAILURE");
			return response;
		}

		DeleteMealsResponse response = new DeleteMealsResponse();
		response.setCode("SUCCESS");

		return response;
	}
}
